package cn.madis.ymcc.web.controller;

import cn.madis.ymcc.dto.KillParamDto;
import cn.madis.ymcc.service.IKillCourseService;
import cn.madis.ymcc.domain.KillCourse;
import cn.madis.ymcc.query.KillCourseQuery;
import cn.madis.ymcc.result.JSONResult;
import cn.madis.ymcc.result.PageList;
import cn.madis.ymcc.vo.Course2OrderMax;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/killCourse")
public class KillCourseController {

    @Autowired
    public IKillCourseService killCourseService;


    /**
     * 订单详情页查询数据展示
     */
    @RequestMapping(value="/online/oneByOrder/{orderNo}",method= RequestMethod.GET)
    public JSONResult oneByOrder(@PathVariable("orderNo") String orderNo){
        Course2OrderMax course2OrderMax =  killCourseService.oneByOrder(orderNo);
        return JSONResult.success(course2OrderMax);
    }


    /**
    * 执行秒杀
    */
    @RequestMapping(value="/kill",method= RequestMethod.POST)
    public JSONResult kill(@RequestBody KillParamDto killParamDto){
      String orderNo =  killCourseService.kill(killParamDto);
        return JSONResult.success(orderNo);
    }
    /**
     * 查询某个秒杀课程上架的
     */
    @RequestMapping(value="/online/one/{killId}/{activityId}",method=RequestMethod.GET)
    public JSONResult one(@PathVariable("killId") Long killId, @PathVariable("activityId") Long activityId ){
        KillCourse killCourse = killCourseService.one(killId,activityId);
        return JSONResult.success(killCourse);
    }
    /**
     * 秒杀课程展示
     */
    @RequestMapping(value="/online/all",method=RequestMethod.GET)
    public JSONResult all(){
        List<KillCourse> all = killCourseService.all();
        return JSONResult.success(all);
    }

    /**
     * 课程加入秒杀
     */
    @RequestMapping(value="/add",method= RequestMethod.POST)
    public JSONResult add(@RequestBody KillCourse killCourse){
        killCourseService.add(killCourse);
        return JSONResult.success();
    }
    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody KillCourse killCourse){
        if(killCourse.getId()!=null){
            killCourseService.updateById(killCourse);
        }else{
            killCourseService.insert(killCourse);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        killCourseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(killCourseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(killCourseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody KillCourseQuery query){
        Page<KillCourse> page = new Page<KillCourse>(query.getPage(),query.getRows());
        page = killCourseService.selectPage(page);
        return JSONResult.success(new PageList<KillCourse>(page.getTotal(),page.getRecords()));
    }
}
